package com.petsbang.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import com.petsbang.action.dao.DataApplication;
import com.petsbang.adapter.lovelyAdapter;
import com.petsbang.common.Common;
import com.petsbang.db.DataBase;
import com.petsbang.vo.PictureInfo;
import com.petsbang.vo.UserShareInfo;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Gallery;
import android.widget.Toast;

public class LovelyActivity extends Activity implements OnItemClickListener ,AdapterView.OnItemSelectedListener{
	public lovelyAdapter lovely;
	public Gallery gallery;
	// 上级页面标志位
	private String pictureTag;
	
	private String fromFlag = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initView();
	}

	private void initView() {
		setContentView(R.layout.lovely);

		DataApplication appState = (DataApplication) getApplicationContext();
		if (appState.getShareList() == null) {
			appState.setShareList(new HashMap<String, ArrayList<UserShareInfo>>());
		}else{
			appState.getShareList().clear();
		}
		
		ImageView back = (ImageView)findViewById(R.id.back);
		back.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		TextView title = (TextView) findViewById(R.id.title);
		title.setText("yyy");
		
		gallery = (Gallery) findViewById(R.id.gallery);

		gallery.setFadingEdgeLength(0);
		gallery.setUnselectedAlpha(1.1f);

		Intent intent = this.getIntent();
		Bundle bundle = intent.getExtras();
		fromFlag = bundle.getString("FromFlag");
		int index = bundle.getInt("index");
		pictureTag = bundle.getString("PictureTag");
		lovely = new lovelyAdapter(this,fromFlag);

		gallery.setAdapter(lovely);
		gallery.setSelection(index);
		gallery.setOnItemSelectedListener(this);
		
		ImageView imageFavor = (ImageView) findViewById(R.id.favor);
		
		imageFavor.setOnClickListener(listenerFavor);
		
		ImageView imageToFavor = (ImageView) findViewById(R.id.download);
		
		imageToFavor.setOnClickListener(listenerToFavor);
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}


	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, final int position, long arg3) {
		
		
	}

	private int showingIndex = -1;
	private static final int TIME_OUT_DISPLAY = 300;
	private int toShowIndex = 0;
	
	@Override
	public void onItemSelected(AdapterView<?> arg0, View arg1, final int position, long arg3) {
		toShowIndex = position;
		final Handler handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				if (showingIndex != toShowIndex) {
					showingIndex = toShowIndex;
					int size = 0;
					DataApplication appState = (DataApplication) LovelyActivity.this.getApplicationContext();
					if (fromFlag == null || "".equals(fromFlag)) {
						size = appState.getPicList().size();
					} else {
						size = appState.getPicFavorList().size();
					}
					
					int index = position;
					int index01 = index > 0 ? (index - 1) : 0;
					int index02 = index >= (size - 1) ? (size - 1) : index + 1;
					lovely.setIndex(index01, index, index02);
				}
			}
		};
		Thread checkChange = new Thread() {
			@Override
			public void run() {
				int myIndex = toShowIndex;
				try {
					sleep(TIME_OUT_DISPLAY);
					if (myIndex == toShowIndex) {
						handler.sendEmptyMessage(0);
					}
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		};

		checkChange.start();
	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub
		
	}
	
	/**
	 * 	
	 */
	ImageView.OnClickListener listenerFavor = new ImageView.OnClickListener(){

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			updateFavor();
		}
	};
	
	private void updateFavor() {
		DataApplication appState = (DataApplication) LovelyActivity.this.getApplicationContext();
		ArrayList<PictureInfo> picInfosList = null;
		if (fromFlag == null || "".equals(fromFlag)) {
			picInfosList = appState.getPicList();
		} else {
			picInfosList = appState.getPicFavorList();
		}
		if (picInfosList != null) {
			PictureInfo tmpPic = picInfosList.get(showingIndex);
			int favorFlag = tmpPic.getFavor();
			if (favorFlag == -1) {
				return;
			}
			if (favorFlag == 0) {
				favorFlag = 1;
			} else {
				favorFlag = 0;
			}
			DataBase db = new DataBase(LovelyActivity.this);
			db.open();
			int result = db.updateFavorPicture(tmpPic.getId(), favorFlag);
			db.close();
			
			if (result != -1 && favorFlag==1) {
				Toast.makeText(LovelyActivity.this,
						"成功收藏！",
						Toast.LENGTH_SHORT).show();
				tmpPic.setFavor(favorFlag);
			} else if (result != -1 && favorFlag==0) {
				Toast.makeText(LovelyActivity.this,
						"取消收藏！",
						Toast.LENGTH_SHORT).show();
				tmpPic.setFavor(favorFlag);
			} else {
				Toast.makeText(LovelyActivity.this,
						"数据库异常！",
						Toast.LENGTH_SHORT).show();
			}

		}
	}
	
	/**
	 * 	
	 */
	ImageView.OnClickListener listenerToFavor = new ImageView.OnClickListener(){

		@Override
		public void onClick(View v) {
			
			Intent intent = new Intent();
			Bundle bundle = new Bundle();
			bundle.putString("PictureTag", pictureTag);
			intent.putExtras(bundle);
			intent.setClass(LovelyActivity.this, FavorActivity.class);
			startActivity(intent);
		}
	};
}